#ifndef GAMEWINDOW_H
#define GAMEWINDOW_H

#include <QMainWindow>
#include <QTcpSocket>
#include <QTcpServer>
#include <QKeyEvent>
#include <QTimer>
#include "highscores.h"
#include "gameconstants.h"
#include "gamesettings.h"

class ModelObserver
{
public:
    virtual void movedInModel(int id, int xPos, int yPos) = 0;
    virtual void destroyedInModel(int id) = 0;
    virtual void createdInModel(int id, Type type, QString img, int xPos, int yPos) = 0;
    virtual void changedFrameInModel(int id, QString newFrame) = 0;
    virtual void increasedScoreInModel(PlayerType player, int n) = 0;
    virtual void setMonkeyToFalling(PlayerType player, bool falling) = 0;
    virtual void monkeyAIShootsCoconut() = 0;
};

namespace Ui {
class GameWindow;
}

class GameWindow : public QMainWindow, ModelObserver
{

    Q_OBJECT

private:
    GameSettings* settings;
    Highscores* highscores;

    bool gamePaused;
    bool gameInProgress;

    // timers
    QTimer* gameTimer;
    QTimer* bananaTimer;

    int time;   // starts with 0 and is increased with the gameTimer hit

    // player's scores
    int player1Score;
    int player2Score;

    // used to measure the time since the last coconut-shot
    int cocoTime1;
    int cocoTime2;

    // true when the respective monkey is falling
    bool monkeyOneFalling;
    bool monkeyTwoFalling;


private slots:
    void keyPressEvent(QKeyEvent* ev);
    void keyReleaseEvent(QKeyEvent* ev);

    // moves objects and processes coconut-hits
    void gameUpdate();

    // sends banana down
    void bananaTimerHit();

    void closeEvent(QCloseEvent* event);

    void on_btnMenu_clicked();

public:
    explicit GameWindow(GameSettings* s, Highscores* h, QWidget *parent = 0);
    ~GameWindow();

    Ui::GameWindow* getUi() {return ui;}

    void setGamePaused(bool b);
    void setGameInProgress(bool b) { gameInProgress = b; }
    bool getGamePaused() { return gamePaused; }
    bool getGameInProgress() { return gameInProgress; }


    // observer
    void movedInModel(int id, int xPos, int yPos);
    void destroyedInModel(int id);
    void createdInModel(int id, Type type, QString img, int xPos, int yPos);
    void changedFrameInModel(int id, QString newFrame);
    void increasedScoreInModel(PlayerType player, int n);
    void setMonkeyToFalling(PlayerType player, bool falling);
    void monkeyAIShootsCoconut();

public slots:

    void startGame();
    void gameOver();
    void resetGameStatus();
    void shootCoconut(PlayerType pl);
    void moveSun(double percent);
    void moveProgressBars();

    // networking methods
    void connectionLost();
    void _client_receivedData(QString data);
    void _server_receivedData(QString data);
    void _client_sendToServer(QString data);
    void _server_sendToClient(QString data);


signals:
    void updateWindowState(bool m, bool g, bool s, bool h);

    // networking signals
    void fromGameWindow_sendToClient(QString data);
    void fromGameWindow_sendToServer(QString data);

private:
    void displayNewScore(PlayerType player, int newScore);

    Ui::GameWindow *ui;

};

#endif // GAMEWINDOW_H
